﻿using System;
using System.Data;
using System.Windows.Forms;
using CXMLStudioIDE.Common.Entities;
using CXMLStudioIDE.DataAccessLayer;

namespace CXMLStudioIDE.Forms
{
    public partial class NewProjectForm : Form
    {
        private readonly MainForm frm_parent;
        private readonly Solution m_solution;

        public NewProjectForm(Form parent)
        {
            InitializeComponent();
            frm_parent = parent as MainForm;
            pnlNewProject.Visible = true;
            var dal = new CXMLStudioDAL();
            dsScope = dal.GetScope();
            cmbScope.DisplayMember = dsScope.Tables[0].Columns[1].ColumnName;
            cmbScope.ValueMember = dsScope.Tables[0].Columns[0].ColumnName;
            cmbScope.DataSource = dsScope.Tables[0];
            m_solution = frm_parent.m_SolutionManager.m_solution;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void cmbScope_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbContext.DataSource = null;
            cmbContext.Items.Clear();
            if (cmbScope.SelectedIndex > 0)
            {
                var dal = new CXMLStudioDAL();
                dsContext = dal.GetContextInScope((int) cmbScope.SelectedValue);
                cmbContext.DisplayMember = dsContext.Tables[0].Columns[1].ColumnName;
                cmbContext.ValueMember = dsContext.Tables[0].Columns[0].ColumnName;
                cmbContext.DataSource = dsContext.Tables[0];
            }
        }

        private void lbTemplates_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txtProjectName.Text.Length == 0 || (cmbContext.SelectedValue == null) ||
                (cmbScope.SelectedValue == null)
                || ((int) cmbContext.SelectedValue <= 0) || ((int) cmbScope.SelectedValue <= 0))
            {
                MessageBox.Show(this, "Please enter Project name, Scope and Context", "CXMLStudioIDE",
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            else
            {
                if (m_solution == null)
                {
                    frm_parent.m_SolutionManager.CreateProject();
                }

                var p = new Project();
                p.Name = txtProjectName.Text;
                if ((int) cmbContext.SelectedValue > 0)
                {
                    p.Context.ID = (int) cmbContext.SelectedValue;
                    p.Context.Name = ((DataRowView) cmbContext.SelectedItem).Row.ItemArray[1].ToString();
                }
                if ((int) cmbScope.SelectedValue > 0)
                {
                    p.Scope.ID = (int) cmbScope.SelectedValue;
                    p.Scope.Name = ((DataRowView) cmbScope.SelectedItem).Row.ItemArray[1].ToString();
                }

                if (m_solution != null && m_solution.Projects != null)
                {
                    var dal = new CXMLStudioDAL();
                    var udal = new UserManagementDAL();
                    var smdal = new SolutionManagerDAL();
                    DataSet ds = smdal.SaveNewProject(frm_parent.m_SolutionManager.m_solution.ID, p.Name,
                                                      udal.GetCXMLUserIdByName(frm_parent.m_CXMLUser), 0, p.Context.ID,
                                                      p.Scope.ID);
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        p.ID = (int) ds.Tables[0].Rows[0].ItemArray[0];
                    }
                    else
                    {
                        MessageBox.Show(this, "Could not save new Project", "CXMLStudioIDE", MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                    }
                    frm_parent.m_SolutionManager.m_solution.AddProjectToSolution(p);
                }
                else
                {
                    frm_parent.m_SolutionManager.m_project = p;
                    frm_parent.EnabledMenuItem("menuItemCloseProject", true);
                }
                //frm_parent.DsBusinessEntityCode = this.dsBusinessEntity;
                frm_parent.UpdateMainForm();
                Dispose();
            }
        }

        private void lblTemplates_Click(object sender, EventArgs e)
        {
        }
    }
}